<%@page pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="com.byComputer.bo.*"%>
<%@page import="com.byComputer.vo.*"%>
<%
	response.setCharacterEncoding("utf-8");
	ComputerService cs = new ComputerService();

	HttpSession s = request.getSession();

	Integer number = (Integer) s.getAttribute("count");

	CartItem cartItem = new CartItem();

	/**这辆购物车用来存放我所购买的电脑的单据(CartItem),单据上面记录了我买了多少台什么型号的电脑*/
	Cart cart = new Cart();

	/**得到我所选购的电脑的ID，用来确认是哪一台电脑*/
	String id = request.getParameter("computerId");
	Integer computerId = 0;
	if (id != null) {
		computerId = Integer.parseInt(id);
		System.out.println("已成功得到所选电脑的ID");
	}
	/**第一次选购的时候要设置下面的参数*/
	if (number == null) {
		number = 1;
		System.out.println("说明是第一次购买");
		/**设置购买了多少电脑*/
		cartItem.setNumber(number);

		/**设置购买的电脑的信息,得首先从数据库中取出电脑信息,也许总是访问数据库会影响效率*/
		Computer computer = cs.getComputer(computerId);
		cartItem.setComputer(computer);
	} else if (computerId == cartItem.getComputer().getId()) {
		/**这里判断这一次选择的是否与上一次的一样，*/
		System.out.println("说明是一样的商品");
		/** 这里记录的是购买电脑的数量*/
		cartItem.setNumber(number);
		number++;
	}

	session.setAttribute("count", number);
	List<Computer> computerList = cs.findAddComputer();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv=Content-Type content="text/html; charset=utf-8" />
		<link href="../css/style.css" type="text/css" rel="stylesheet" />
	</head>

	<body topMargin="10">
		<div id="append_parent"></div>
		<table cellSpacing=6 cellPadding=2 width="100%" border="0">

			<tbody>
				<tr>
					<td>
						<table class="tableborder" cellSpacing="0" cellPadding="0"
							width="100%" border="0">
							<tbody>

								<tr class="header">
									<td class="altbg1" width="15%">
										<b>:型号</B>
									</td>
									<td class="altbg1" width="20%">
										<b>产品图片</b>
									</td>
									<td class="altbg1" width="30%">
										<b>产品说明</b>
									</td>

									<td class="altbg1" width="10%">
										<b>产品报价</b>
									</td>
									<td class="altbg1">
										&nbsp;
									</td>

								</tr>
							</tbody>
							<tbody>
								<%
									for (Computer computer : computerList) {
								%>
								<tr>
									<td class="altbg2">
										<%=computer.getModel()%>
									</td>
									<td class="altbg2">
										<img src="../image/<%=computer.getPicture()%>" width="150"
											height="90" />
									</td>
									<td class="altbg2">
										<%=computer.getDescription()%>
									</td>

									<td class="altbg2">
										￥
										<%=computer.getPrice()%>
									</td>
									<td class="altbg2">
										<a href="computer_list.jsp?computerId=<%=computer.getId()%>">
											购买</a> &nbsp;&nbsp;&nbsp;
									</td>
								</tr>
								<%--在链接的地址后面加上"?id=..."可以达到submit同样的效果,将我们需要的数据带到--%>
								<%
									}
								%>


							</tbody>
						</table>
						<br />
						<center>

							<input class="button" type="button" value="查看购物车"
								name="settingsubmit" onclick="location = 'cart.jsp';">
						</center>

						<center>

							<input class="button" type="button" value="测试地址"
								name="settingsubmit" onclick="location = 'cart_item.jsp';">
						</center>

					</td>
				</tr>
			</tbody>
		</table>

	</body>
</html>
